/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPnlImg.java
 *
 * Created on 14/07/2010, 15:56:42
 */
package systole.view.tabs.segments;

import java.awt.Color;
import java.awt.Image;
import java.awt.event.MouseEvent;
import javax.swing.border.Border;
import org.jdesktop.swingx.JXImagePanel.Style;
import org.jdesktop.swingx.border.DropShadowBorder;
import org.jdesktop.application.Action;

/**
 *
 * @author jmj
 */
public class JPnlImg extends javax.swing.JPanel {

    private Color defaultColor;
    private boolean selected = false;
    private boolean clicked = false;
    private JPnlImageMap parent;
    private Border defaultBorder;
    private Border selectedBorder;
    private Image  image;
    private Color selectedColor = new Color(192, 192, 192);

    /** Creates new form JPnlImg */
    public JPnlImg(Image image, JPnlImageMap parent, int segmentNumber) {
        initComponents();
        this.setFocusable(true);
        
        DropShadowBorder selectBorder = new DropShadowBorder();
        selectBorder.setShowLeftShadow(true);
        selectBorder.setShowTopShadow(true);

        this.parent = parent;
        this.image = image;
        this.jXImagePanel.setImage(image);
        this.jXImagePanel.setStyle(Style.CENTERED);
        this.jXImagePanel.setEditable(false);
        this.defaultColor = this.jPnlBottom.getBackground();
        this.defaultBorder = this.getBorder();
        this.selectedBorder = selectBorder;
        this.setComponentPopupMenu(jPopupMenuImg);
        this.jXImagePanel.setComponentPopupMenu(jPopupMenuImg);
        this.setFocusable(false);
        this.jLblSegmentNumber.setText("segmento: " + segmentNumber);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenuImg = new javax.swing.JPopupMenu();
        jMenuSelect = new javax.swing.JMenuItem();
        jMenuItUnselect = new javax.swing.JMenuItem();
        jMenuICorrelation = new javax.swing.JMenuItem();
        jPnlTop = new javax.swing.JPanel();
        jLblSegmentNumber = new javax.swing.JLabel();
        jPnlBottom = new javax.swing.JPanel();
        jPnlRigth = new javax.swing.JPanel();
        jPnlLeft = new javax.swing.JPanel();
        jXImagePanel = new org.jdesktop.swingx.JXImagePanel();

        jPopupMenuImg.setName("jPopupMenuImg"); // NOI18N
        jPopupMenuImg.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                jPopupMenuImgPopupMenuWillBecomeVisible(evt);
            }
        });
        jPopupMenuImg.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jPopupMenuImgComponentShown(evt);
            }
        });

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(systole.view.SystoleApp.class).getContext().getActionMap(JPnlImg.class, this);
        jMenuSelect.setAction(actionMap.get("selectPlot")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(systole.view.SystoleApp.class).getContext().getResourceMap(JPnlImg.class);
        jMenuSelect.setIcon(resourceMap.getIcon("jMenuSelect.icon")); // NOI18N
        jMenuSelect.setText(resourceMap.getString("jMenuSelect.text")); // NOI18N
        jMenuSelect.setToolTipText(resourceMap.getString("jMenuSelect.toolTipText")); // NOI18N
        jMenuSelect.setName("jMenuSelect"); // NOI18N
        jPopupMenuImg.add(jMenuSelect);

        jMenuItUnselect.setAction(actionMap.get("unselectPlot")); // NOI18N
        jMenuItUnselect.setIcon(resourceMap.getIcon("jMenuItUnselect.icon")); // NOI18N
        jMenuItUnselect.setText(resourceMap.getString("jMenuItUnselect.text")); // NOI18N
        jMenuItUnselect.setToolTipText(resourceMap.getString("jMenuItUnselect.toolTipText")); // NOI18N
        jMenuItUnselect.setName("jMenuItUnselect"); // NOI18N
        jPopupMenuImg.add(jMenuItUnselect);

        jMenuICorrelation.setAction(actionMap.get("correlation")); // NOI18N
        jMenuICorrelation.setIcon(resourceMap.getIcon("jMenuICorrelation.icon")); // NOI18N
        jMenuICorrelation.setText(resourceMap.getString("jMenuICorrelation.text")); // NOI18N
        jMenuICorrelation.setToolTipText(resourceMap.getString("jMenuICorrelation.toolTipText")); // NOI18N
        jMenuICorrelation.setName("jMenuICorrelation"); // NOI18N
        jPopupMenuImg.add(jMenuICorrelation);

        setBorder(javax.swing.BorderFactory.createLineBorder(resourceMap.getColor("Form.border.lineColor"), 2)); // NOI18N
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(180, 180));
        setLayout(new java.awt.BorderLayout());

        jPnlTop.setMaximumSize(new java.awt.Dimension(32767, 15));
        jPnlTop.setMinimumSize(new java.awt.Dimension(0, 15));
        jPnlTop.setName("jPnlTop"); // NOI18N
        jPnlTop.setPreferredSize(new java.awt.Dimension(400, 15));
        jPnlTop.setLayout(new java.awt.BorderLayout());

        jLblSegmentNumber.setFont(jLblSegmentNumber.getFont().deriveFont(jLblSegmentNumber.getFont().getSize()-1f));
        jLblSegmentNumber.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLblSegmentNumber.setText(resourceMap.getString("jLblSegmentNumber.text")); // NOI18N
        jLblSegmentNumber.setName("jLblSegmentNumber"); // NOI18N
        jPnlTop.add(jLblSegmentNumber, java.awt.BorderLayout.CENTER);

        add(jPnlTop, java.awt.BorderLayout.PAGE_START);

        jPnlBottom.setMaximumSize(new java.awt.Dimension(32767, 5));
        jPnlBottom.setMinimumSize(new java.awt.Dimension(0, 5));
        jPnlBottom.setName("jPnlBottom"); // NOI18N
        jPnlBottom.setPreferredSize(new java.awt.Dimension(400, 5));

        javax.swing.GroupLayout jPnlBottomLayout = new javax.swing.GroupLayout(jPnlBottom);
        jPnlBottom.setLayout(jPnlBottomLayout);
        jPnlBottomLayout.setHorizontalGroup(
            jPnlBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 186, Short.MAX_VALUE)
        );
        jPnlBottomLayout.setVerticalGroup(
            jPnlBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 5, Short.MAX_VALUE)
        );

        add(jPnlBottom, java.awt.BorderLayout.PAGE_END);

        jPnlRigth.setMaximumSize(new java.awt.Dimension(5, 32767));
        jPnlRigth.setMinimumSize(new java.awt.Dimension(5, 0));
        jPnlRigth.setName("jPnlRigth"); // NOI18N
        jPnlRigth.setPreferredSize(new java.awt.Dimension(5, 195));

        javax.swing.GroupLayout jPnlRigthLayout = new javax.swing.GroupLayout(jPnlRigth);
        jPnlRigth.setLayout(jPnlRigthLayout);
        jPnlRigthLayout.setHorizontalGroup(
            jPnlRigthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 5, Short.MAX_VALUE)
        );
        jPnlRigthLayout.setVerticalGroup(
            jPnlRigthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 140, Short.MAX_VALUE)
        );

        add(jPnlRigth, java.awt.BorderLayout.LINE_END);

        jPnlLeft.setMaximumSize(new java.awt.Dimension(5, 32767));
        jPnlLeft.setMinimumSize(new java.awt.Dimension(5, 0));
        jPnlLeft.setName("jPnlLeft"); // NOI18N
        jPnlLeft.setPreferredSize(new java.awt.Dimension(5, 195));

        javax.swing.GroupLayout jPnlLeftLayout = new javax.swing.GroupLayout(jPnlLeft);
        jPnlLeft.setLayout(jPnlLeftLayout);
        jPnlLeftLayout.setHorizontalGroup(
            jPnlLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 5, Short.MAX_VALUE)
        );
        jPnlLeftLayout.setVerticalGroup(
            jPnlLeftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 140, Short.MAX_VALUE)
        );

        add(jPnlLeft, java.awt.BorderLayout.LINE_START);

        jXImagePanel.setName("jXImagePanel"); // NOI18N
        jXImagePanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jXImagePanelMouseClicked(evt);
            }
        });
        jXImagePanel.setLayout(new java.awt.BorderLayout());
        add(jXImagePanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jPopupMenuImgComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jPopupMenuImgComponentShown
    }//GEN-LAST:event_jPopupMenuImgComponentShown

    private void jPopupMenuImgPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jPopupMenuImgPopupMenuWillBecomeVisible
        this.jMenuItUnselect.setEnabled(this.selected);
        this.jMenuSelect.setEnabled(!this.selected);
    }//GEN-LAST:event_jPopupMenuImgPopupMenuWillBecomeVisible

    private void jXImagePanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jXImagePanelMouseClicked
         if ((evt.getClickCount() == 1) && (evt.getButton() == MouseEvent.BUTTON1)) {
            this.click();
            return;
        }

        if ((evt.getClickCount() == 2) && (evt.getButton() == MouseEvent.BUTTON1)) {
            this.select();
            return;
        }

    }//GEN-LAST:event_jXImagePanelMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLblSegmentNumber;
    private javax.swing.JMenuItem jMenuICorrelation;
    private javax.swing.JMenuItem jMenuItUnselect;
    private javax.swing.JMenuItem jMenuSelect;
    private javax.swing.JPanel jPnlBottom;
    private javax.swing.JPanel jPnlLeft;
    private javax.swing.JPanel jPnlRigth;
    private javax.swing.JPanel jPnlTop;
    private javax.swing.JPopupMenu jPopupMenuImg;
    private org.jdesktop.swingx.JXImagePanel jXImagePanel;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the selected
     */
    public boolean isSelected() {
        return selected;
    }

    /**
     * @param selected the selected to set
     */
    protected void setSelected(boolean selected) {
        this.selected = selected;
    }

    /**
     * @return the clicked
     */
    protected boolean isClicked() {
        return clicked;
    }

    /**
     * @param clicked the clicked to set
     */
    protected void setClicked(boolean clicked) {
        this.clicked = clicked;
    }

    protected void select() {
        if (!this.selected) {
            this.jPnlBottom.setBackground(this.selectedColor);
            this.jPnlLeft.setBackground(this.selectedColor);
            this.jPnlRigth.setBackground(this.selectedColor);
            this.jPnlTop.setBackground(this.selectedColor);
            this.revalidate();
            this.setSelected(true);
            this.parent.select(this);
        } else {
            unselect();
        }
    }

    protected void unselect() {
        if (this.selected) {
            this.jPnlBottom.setBackground(this.defaultColor);
            this.jPnlLeft.setBackground(this.defaultColor);
            this.jPnlRigth.setBackground(this.defaultColor);
            this.jPnlTop.setBackground(this.defaultColor);
            this.revalidate();
            this.setSelected(false);
            this.parent.unselect(this);
        }
    }

    protected void click() {
        if (!this.isClicked()) {
            this.setBorder(this.selectedBorder);
            this.revalidate();
            this.setFocusable(true);
            this.jXImagePanel.revalidate();
            this.setClicked(true);
            this.parent.clickOnImage(this);
            this.requestFocusInWindow();
        }
    }

    protected void unclikc() {
        if (this.isClicked()) {
            this.setBorder(this.defaultBorder);
            this.revalidate();
            this.setClicked(false);
        }
    }

    @Action
    public void selectPlot() {
        this.select();
    }

    @Action
    public void unselectPlot() {
        this.unselect();
    }

    @Action
    public void correlation() {
        this.parent.applyCorrelation(this);
    }

}
